<?php

class configurationActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $this->configuration = ConfigurationPeer::doSelectOne(new Criteria());
    }

    public function executeSave(sfWebRequest $request){
        $username = $request->getParameter('username');
        $number_of_pages = $request->getParameter('number_of_pages');
        $configuration = ConfigurationPeer::doSelectOne(new Criteria());
        $configuration->setAdminUser($username);
        $configuration->setNumberOfPages($number_of_pages);
        $configuration->save();

        $this->getUser()->setFlash('msg','Datos actualizados satisfactoriamente');
        $this->redirect('@configuration');
    }

    public function executeChange_password(sfWebRequest $request){
        
    }

    public function executeChange_password_save(sfWebRequest $request){
        $old = $request->getParameter('old');
        $new = $request->getParameter('new');
        $repeat = $request->getParameter('repeat');

        if($old =="" || $new =="" || $repeat ==""){
            $this->getUser()->setFlash('msg','Los datos ingresados no pueden estar vacíos');
            $this->redirect('@change_password');
        }
        if($new!=$repeat){
            $this->getUser()->setFlash('msg','Repita la contraseña correctamente');
            $this->redirect('@change_password');
        }

        $configuration = ConfigurationPeer::doSelectOne(new Criteria());

        if(md5($old)!=$configuration->getAdminPassword()){
            $this->getUser()->setFlash('msg','La contraseña ingresada es incorrecta');
            $this->redirect('@change_password');
        }

        $configuration->setAdminPassword(md5($new));
        $configuration->save();

        $this->getUser()->setFlash('msg','La contraseña fue modificada exitosamente');
        $this->redirect('@configuration');
    }

}